var countries = [];
var areaInfo = null;
function initAreaInfo() {
	DWRFunction.getCountries(function(data) {
		countries = eval(data);

		var opType = $("#opTypeHidden").val();
		if (opType == "DETAIL") {
			displayAreaInfo();
		} else {
			var cntryCd = $("#cntryCdHidden").val();
			if (cntryCd != "") {
				for (var i = 0; i < countries.length; i ++) {
					if (countries[i].cd == cntryCd) {
						$("#cntryName").val(countries[i].name);
						break;
					}
				}
			} else {
				$("#cntryCdHidden").val("156");
				$("#cntryName").val("156-中国");
			}
			getProvincesByCountry($("#cntryCdHidden").val());
		}
	});
}
function displayAreaInfo() {
	var cntryCd = $("#cntryCdHidden").val();
	for (var i = 0; i < countries.length; i ++) {
		if (countries[i].cd == cntryCd) {
			$("#countryDiv").html(countries[i].name);
			break;
		}
	}
	var areaCd = $("#divisionCdHidden").val();
	DWRFunction.getAreaInfo(areaCd, function(data) {
		areaInfo = eval(data);
		var lv = areaInfo[0].level;
		if (lv == "1") {
			$("#provinceDiv").html(areaInfo[0].cnNm);
		} else if (lv == "2") {
			$("#provinceDiv").html(areaInfo[0].provNm);
			$("#cityDiv").html(areaInfo[0].cnNm);
		}
	});
}
// get provinces by country
function getProvincesByCountry(cntry) {
	if (cntry == "156") {
		DWRFunction.getProvincesByCntry(cntry, function(data) {
			DWRUtil.removeAllOptions("provSelection");
			if(data != null) {
				DWRUtil.addOptions("provSelection",{'':'--请选择--'});
				DWRUtil.addOptions("provSelection", data);
				var areaCd = $("#divisionCdHidden").val();
				if (areaCd != "") {
					if (areaInfo == null) {
						DWRFunction.getAreaInfo(areaCd, function(data) {
							areaInfo = eval(data);
							initCities(areaCd);
						});
					} else {
						initCities(areaCd);
					}
				}
			}
		});
	} else {
		DWRUtil.removeAllOptions("provSelection");
		DWRUtil.removeAllOptions("citySelection");
		$("#divisionCdHidden").val("");
	}
}
function initCities(areaCd) {
	if (areaInfo.length == 1) {
		if (areaInfo[0].level == '1') {
			$("#provSelection option[value=" + areaCd + "]").attr("selected", "selected");
			getCitiesByProv(areaCd, null);
		} else if (areaInfo[0].level == '2') {
			$("#provSelection option[value=" + areaInfo[0].provCd + "]").attr("selected", "selected");
			getCitiesByProv(areaInfo[0].provCd, areaCd);
		}
	}
}
function getCitiesByProv(prov, selCity) {
	DWRFunction.getCitesByProvince(prov, function(data) {
		DWRUtil.removeAllOptions("citySelection");
		if (data != null) {
			DWRUtil.addOptions("citySelection",{'':'--请选择--'});
			DWRUtil.addOptions("citySelection", data);
			if (selCity != null) {
				$("#citySelection option[value=" + selCity + "]").attr("selected", "selected");
			}
		}
	});
}
function chooseProv(ele) {
	$("#divisionCdHidden").val($(ele).val());
	getCitiesByProv($(ele).val());
}
function chooseCity(ele) {
	if ($(ele).val() != "") {
		$("#divisionCdHidden").val($(ele).val());
	} else {
		$("#divisionCdHidden").val($("#provSelection").val());
	}
}
function showCountries(ele) {
	var c = $(ele).val();
	$("#countriesDiv ul").html("");
	if (countries.length > 0) {
		var show = false;
		var r = 0;
		for(var i=0;i<countries.length;i++) {
			var j = countries[i];
			if (j.name.indexOf($.trim(c)) != -1) {
				show = true;
				$("#countriesDiv ul").append("<li onmouseover='$(this).addClass(\"countryItem\");' " +
						"onmouseout='$(this).removeClass(\"countryItem\");'" +
						"key='" + j.cd + "'>" + j.name + "</li>");
				r ++;
			}
		}

		if (show) {
			var offs = $(ele).offset();
			var t = offs.top;
			var l = offs.left;
			var h = $(ele).height();
			$("#countriesDiv").css({'top':t + h + 5, 'left' : l}).show();
			
			if (r == 1) {
				$("#countriesDiv ul li").addClass("countryItem");
			}
			
			$("#countriesDiv ul li").click(function(event) {
				event.preventDefault();
				event.stopPropagation();
				
				$("#cntryCdHidden").val($(this).attr("key"));
				$("#cntryName").val($(this).html());
				getProvincesByCountry($(this).attr("key"));
				hideProvince();
				$("#cntryName").focus();
			});
		} else {
			$("#countriesDiv").hide();
		}
	} else {
		$("#countriesDiv").hide();
	}
}
function hideProvince() {
	$("#countriesDiv ul").html("");
	$("#countriesDiv").hide();
}

$(document).ready(function() {
	if (document.getElementById("cntryCdHidden") != null 
			&& document.getElementById("divisionCdHidden") != null) {
		$("#cntryName").click(function(event) {
			event.preventDefault();
			event.stopPropagation();
		});
		
		$("body").append("<div id='countriesDiv'><iframe id='countriesDivIframe' frameborder='0' src='about:blank'></iframe><ul></ul></div>")
				 .click(function() {
					 if (document.getElementById("cntryName") != null) {
						 if ($("#countriesDiv ul li").length == 1) {
							 $("#countriesDiv ul li").click();
						 } else {
							 if ($("#cntryName").val() == "") {
								 $("#cntryName").val("");
								 $("#cntryCdHidden").val("");
								 DWRUtil.removeAllOptions("provSelection");
								 DWRUtil.removeAllOptions("citySelection");
								 $("#divisionCdHidden").val("");
							 } else {
								 var cntryCd = $("#cntryCdHidden").val();
								 var flg = false;
								 for (var i = 0; i < countries.length; i ++) {
									 if (countries[i].cd == cntryCd) {
										 flg = true;
										 if ($("#cntryName").val() != countries[i].name) {
											$("#cntryName").val(countries[i].name);
											alert("请输入合法的国家");
											break;
										 }
									 }
								 }
								 if (!flg) {
									 $("#cntryName").val("");
									 $("#cntryCdHidden").val("");
								 }
							 }
						 }
						 hideProvince();
					 }
		});
		initAreaInfo();
	}
});